/**
 * this is the class for handling and internal storage of http request data
 * given a httpChannel it will parse out the basics via the constructor
 * 
 * Everything is stored splitted to be easily accessed for fuzzing
 * 
 */
 //TODO:move the header parsing in to the main Request class 
 
function Request (httpChannel)
{  
  /* 
   this.ignored = new Array(".gif", ".jpg", ".css", ".png"); 

   this.ignored.forEach( function(filetype){
        if (this.Url.indexOf(filetype) == -1)
            alert("ignore me");
    });    
   }
  
    */
    
    this.id = 0; //this is set later when we add it to a list;
    this.Url = httpChannel.URI ? httpChannel.URI.asciiSpec : null; 
	this.Timestamp = new Date();
	this.RequestHeaders = new Array();
	this.ResponseHeaders = new Array();
	this.log = true;
		
	// get properties from httpchannel/request object
	this.Status = httpChannel.status ? httpChannel.status : null;
	
	this.nsiURI = httpChannel.URI ? httpChannel.URI : null; // weak reference?
	this.Name = httpChannel.name ? httpChannel.name : null;
	this.Owner = httpChannel.owner ? httpChannel.owner : null;
	this.SecurityInfo = httpChannel.securityInfo ? httpChannel.securityInfo : null;
	this.RequestMethod = httpChannel.requestMethod ? httpChannel.requestMethod : null;
	this.IsPending = httpChannel.isPending();
	this.Referrer = httpChannel.referrer ? httpChannel.referrer : null;
	this.LoadGroup = httpChannel.loadGroup;
	this.LoadFlags = httpChannel.loadFlags;
	this.ProxyInfo = httpChannel.proxyInfo ? httpChannel.proxyInfo : null;
	this.Priority = httpChannel.priority ? httpChannel.priority : null;
	this.IsBackground = this.LoadFlags & Components.interfaces.nsIRequest.LOAD_BACKGROUND;
		
	try { this.ContentType = httpChannel.contentType; } catch(ex) {}
	try { this.ContentCharset = httpChannel.ContentCharset; } catch(ex) {}
	try { this.ContentLength = httpChannel.contentLength; } catch(ex) {}
	try { this.RequestSucceeded = httpChannel.requestSucceeded; } catch(ex) {}
	try { this.ResponseStatus = httpChannel.responseStatus; } catch(ex) {}
	try { this.ResponseStatusText = httpChannel.responseStatusText; } catch(ex) {}
	try { this.IsNoStoreResponse = httpChannel.isNoStoreResponse(); } catch(ex) {}
	try { this.IsNoCacheResponse = httpChannel.isNoCacheResponse(); } catch(ex) {}
	try { this.EntityId = httpChannel.EntityId; } catch(ex) {}
	try { this.getQueryString();} catch(ex) {}
	
	this.toString = function()
	{
	    return this.Timestamp + ": " + this.RequestMethod + " "+ this.Url + ", " + this.ResponseStatus;    
	}
	
	this.getQueryString = function() 
	{
	    if (this.Url.indexOf("?") == -1) 
		  return;
		var parts = this.Url.split();
		this.baseUrl = parts[0];
		this.QueryString = this.Url.slice(this.Url.indexOf("?") + 1, this.Url.length);
		this.QueryStringParameters = new Array();
		var queryStringParts = this.QueryString.split("&");
		for (i in queryStringParts)
		{
			var nameValuePair = queryStringParts[i].split("=");
			var nvName = queryStringParts[i].slice(0, queryStringParts[i].indexOf("="));
			var nvValue = (queryStringParts[i].indexOf("=") != -1) ? queryStringParts[i].slice(queryStringParts[i].indexOf("=") + 1, queryStringParts[i].length) : "";
			this.QueryStringParameters[nvName] = nvValue;
		}
	}	
}

function HeaderInfo() // TODO: this needs to be refactored to match new coding style
{
	this.init();
}

HeaderInfo.prototype = // TODO: this needs to be refactored to match new coding style
{
	Headers: null,
	
	init: function()
	{
		this.Headers = new Array();
	},
	
	visitHeader: function(name, value)
	{
		this.Headers[name] = value;
	},	
}

